import api from "../../utils/api_util";
import baseUtil from "../../utils/base_util";
import tips from "../../utils/tip";

const app = getApp();
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        images: [],
        rating: {
            id: 0,
            scores: 0,
        },
        detailData: '',
        content: '',
        //商品评分
        goodsScore: '',
        //物流评分
        logisticsScore: '',
        //客服评分
        serviceScore: '',
        baseUrl: '',

    },
    onShow: function () {
        this.getOrderDetail();
    },
    //商品评分
    setScores: function (e) {
        var id = e.currentTarget.dataset.id;
        console.log(id);
        var index = e.currentTarget.dataset.index;
        console.log(index);

        this.data.rating.scores = index;
        console.log(this.data.rating);
        this.setData({
            rating: this.data.rating,
            goodsScore: index,
        })
    },
    //物流评分
    setlogisticsScores: function (e) {
        var id = e.currentTarget.dataset.id;
        console.log(id);
        var index = e.currentTarget.dataset.index;
        console.log(index);

        this.setData({
            logisticsScore: index,
        })
    },
    //客服评分
    setserviceScores: function (e) {
        var id = e.currentTarget.dataset.id;
        console.log(id);
        var index = e.currentTarget.dataset.index;
        console.log(index);

        this.setData({
            serviceScore: index,
        })
    },
    //图片上传
    chooseImage(e) {
        let that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: res => {

                that.doUpload(res.tempFilePaths);
            }
        })
    },
    doUpload: function (tempFilePaths) {
        let that = this;
        wx.uploadFile({
            url: api.urls.fileUpload,
            filePath: tempFilePaths[0],
            name: 'file',
            // formData: {
            //     'id': that.roomId
            // },
            header: {
                'token': baseUtil.getStorageSync('token') ? baseUtil.getStorageSync('token') : '',
            },
            success(res) {
                // tipUtil.loaded();
                let data = JSON.parse(res.data);
                let images = [...that.data.images, api.urls.imgUrl + data.data.file[0]];
                that.setData({
                    images: images
                });
                console.log('图片:', that.data.images)

            },
        })
    },
    removeImage(e) {
        let iindex = e.target.dataset.index;
        this.setData({
            images: this.data.images.filter((item, index) => index != iindex)
        })
        wx.showToast({
            title: "图片已删除"
        })
    },
    //订单详情
    getOrderDetail() {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.orderDetail, {
            method: 'GET',
            data: {
                id: baseUtil.getCurrentUrlParam('orderId'),
                // id: 28,
            },
            success: function (res) {

                res.data.createTime = baseUtil.formatDateTime(res.data.createTime);

                that.setData({
                    detailData: res.data
                })
            },
        })
    },
    //评论订单
    createCommentPost() {
        let that = this;
        api.wxRequest.apiRequest(that, api.urls.createComment, {
            method: 'POST',
            data: {
                v: "1.0.0",
                goodId: that.data.detailData.goodsList[0].id,
                orderId: baseUtil.getCurrentUrlParam('orderId'),
                anonymity: "0",  //是否匿名
                goodsScore: that.data.goodsScore,
                logisticsScore: that.data.logisticsScore,
                serviceScore: that.data.serviceScore,
                content: that.data.content,
                images: that.data.images,
            },
            success: function (res) {
                tips.alert('评论成功~');
                setTimeout(function () {
                    baseUtil.pageJump('../OrderList/OrderList', true)
                }, 500)
            },
        })
    },
    //评论内容
    textareaChange: function (e) {
        this.setData({
            content: e.detail.value,
        })
    }
})